package com.stylefeng.guns.core.util.appSecure;

import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;


import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.core.util.appSecure.encoder.UtilAES;

import net.sf.json.JSONObject;


public class RSATest {
	
	//@Test
	public void testRSA() {
		try {
			String test = "测试数据";
			byte[] en0 = RSAUtil.encrypt(test.getBytes()); //加密
			byte[] de0 = RSAUtil.decrypt(en0); //解密  Base64编码成字符串
			System.out.println("加密0：" + HexUtil.encodeHexStr(en0)); //转16
//			System.out.println("解密0：" + RSAUtil.bytesToString(de0));
			
			KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
			keyPairGen.initialize(2048, new SecureRandom("BroadDeep".getBytes()));
			KeyPair keyPair = keyPairGen.generateKeyPair();
			RSAPublicKey pu = (RSAPublicKey) keyPair.getPublic();
			RSAPrivateKey pr = (RSAPrivateKey) keyPair.getPrivate();

			System.out.println("-------- 测试 1 ------");
//			System.out.println("pr encode:" + HexUtil.encodeHexStr(pr.getEncoded()));
			//System.out.println("pr encode:" + Base64.encodeToString(pr.getEncoded(), Base64.DEFAULT));
			RSAPrivateKey privateKey = RSAUtil.getPrivateKey(pr.getEncoded());  //**************
//			System.out.println("pu encode:" + HexUtil.encodeHexStr(pu.getEncoded()));
			//System.out.println("pu encode:" + Base64.encodeToString(pu.getEncoded(), Base64.DEFAULT));
			RSAPublicKey publicKey = RSAUtil.getPublicKey(pu.getEncoded()); //*******************

			byte[] en1 = RSAUtil.encrypt(publicKey, test.getBytes());
			byte[] de1 = RSAUtil.decrypt(privateKey, en1);
//			System.out.println("加密1：" + HexUtil.encodeHexStr(en1));
			//System.out.println("加密1：" + Base64.encodeToString(en1, Base64.DEFAULT));
			System.out.println("解密1：" + RSAUtil.bytesToString(de1));

			System.out.println("-------- 测试 2 ------");
			BigInteger priModulus = pr.getModulus();
			BigInteger priExponent = pr.getPrivateExponent();
			System.out.println("priModulus：" + priModulus);
			System.out.println("priExponent：" + priExponent);

			BigInteger pubModulus = pu.getModulus();
			BigInteger pubExponent = pu.getPublicExponent();
			System.out.println("pubModulus：" + pubModulus);
			System.out.println("pubExponent：" + pubExponent);

			PublicKey pubKey = RSAUtil.getPublicKey(pubModulus.toString(), pubExponent.toString());
			PrivateKey priKey = RSAUtil.getPrivateKey(priModulus.toString(), priExponent.toString());
			byte[] en2 = RSAUtil.encrypt(pubKey, test.getBytes());
			byte[] de2 = RSAUtil.decrypt(priKey, en2);
//			System.out.println("加密2：" + HexUtil.encodeHexStr(en2));
			//System.out.println("加密2：" + Base64.encodeToString(en2, Base64.DEFAULT));
			System.out.println("解密2：" + RSAUtil.bytesToString(de2));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//@Test  //模拟手机端
	public void SearchMarkNumberTest() throws Exception {
    	String appid = "90a37a82-d949-4c99-982f-832b7179db00";
    	String echostr = "123456";
    	String timestamp = "1504504378482";
    	String sign;
    	
    	String pwd = "d8623ac47ca33345e774de46f53ffeae";
    	
    	// 101  13810426631  15722298722  13210359966
    	String req_body = UtilAES.base64Encode(UtilAES.encrypt("{'phonelist': [ { 'phone_num':'15340128888'}]  }".getBytes(),"d8623ac47ca33345e774de46f53ffeae"));
    	String url = "http://xxxx:8080/guns/?req_body="+URLEncoder.encode(req_body, "UTF-8"); //服务端不需要解码
	  
	    //get签名
    	String signStr = appid+echostr+timestamp+pwd;
    	String signStrMD5 = HashUtil.md5(signStr.getBytes()); 
    	sign = signStrMD5.substring(signStrMD5.length()-16,signStrMD5.length()).toLowerCase();
		
		//http头
		Map<String, Object> headersMap  = new HashMap<String, Object>();
		headersMap.put("appid",appid);
		headersMap.put("echostr",echostr);
		headersMap.put("timestamp",timestamp);
		headersMap.put("sign",sign);
		
		String resultInfo = HttpClientUtil.doPost(url, headersMap, null);
		System.out.println("test请求结果=============:"+resultInfo);
		
        JSONObject jsonObject = new JSONObject();
		jsonObject = JSONObject.fromObject(resultInfo);
		System.out.println("phone_attr======:"+jsonObject.getString("phone_attr"));
		String resultInfoDec = new String(UtilAES.decrypt(UtilAES.base64Decode(jsonObject.getString("phone_attr").toString()),"d8623ac47ca33345e774de46f53ffeae"),"UTF-8");
        System.out.println("test返回结果解密后====:"+resultInfoDec);
	}
	
	
	
}
